﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace DataBinding
{
    public partial class MobileClone : System.Web.UI.Page
    {
        List<Producer> producerss;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack) 
            {
                producerss = new List<Producer>();
                
                Producer producer = new Producer { Name = "BMW", Models = new List<string> { "X5", "M5", "Z9" } };
                producerss.Add(producer);
                producer = new Producer { Name = "Ford", Models = new List<string> { "Mondeo", "Fiesta", "Esscort" } };
                producerss.Add(producer);
                producer = new Producer { Name = "Toyota", Models = new List<string> { "Corola", "IQ", "Aygo", "Avensis" } };
                producerss.Add(producer);
                producer = new Producer { Name = "Nissan", Models = new List<string> { "Gtr", "350-Z", "370-Z" } };
                producerss.Add(producer);
                this.Producers.DataSource = producerss;
                this.Producers.DataBind();
                this.Producers.SelectedIndex = 1;
                int selected = this.Producers.SelectedIndex;
                Producer selectedProducer = producerss[selected];
                this.Models.DataSource = selectedProducer.Models;
                this.Models.DataBind();
                Session.Add("prod", producerss);
            }
            
            List<string> extras = new List<string> {"AC","Central Locing", "el. Mirrors","19 inch rims","200w Stereo", "Play station3" };
            if (!Page.IsPostBack)
            {
                this.Extras.DataSource = extras;
                this.Extras.DataBind();
                this.Extras.RepeatColumns = 4;
            }

            string[] engines = new string[] {"1.4","1.4 D","2.0 TDI", "3L twin turbo"};
            if (!Page.IsPostBack)
            {
                this.Engines.DataSource = engines;
                this.Engines.DataBind();
                this.Engines.RepeatDirection = RepeatDirection.Horizontal;
            }

        }

        protected void Submit_Clicked(object sender, EventArgs e) 
        {
            StringBuilder result = new StringBuilder();
            result.AppendLine("producer:" + this.Producers.SelectedItem.Text);
            result.AppendLine("Model:" + this.Models.SelectedItem.Text);
            result.Append("SelectedExtras:(");
            for (int i = 0; i < this.Extras.Items.Count; i++)
            {
                if (this.Extras.Items[i].Selected)
                {
                    result.AppendLine(this.Extras.Items[i].Text);  
                }
            }
            result.Append(")");
            result.AppendLine("Engine:" + this.Engines.SelectedItem.Text);
            this.Result.Text = Server.HtmlEncode(result.ToString());
        
        }
        protected void Change_Producer(object sender, EventArgs e) 
        {

            int selected = this.Producers.SelectedIndex;
            Producer selectedProducer = ((List<Producer>) Session["prod"])[selected];
            this.Models.DataSource = selectedProducer.Models;
            this.Models.DataBind();
        }
    

    }
}